package com.tchintech.pay.view.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.tchintech.pay.R;
import com.tchintech.pay.adapter.FullyGridLayoutManager;
import com.tchintech.pay.adapter.RecycleCodeAdapter;
import com.tchintech.pay.adapter.RecyclePayAdapter;
import com.tchintech.pay.adapter.SpaceBaseItemDecoration;
import com.tchintech.pay.delegate.TeleChargeDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.model.entity.PayFees;
import com.tchintech.pay.utils.EditUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/22 0022.
 * <p>
 * 话费充值
 */

public class TeleChargeActivity extends ActivityPresenter<TeleChargeDelegate> implements View.OnClickListener {
    private RecyclerView recyclerView;
    private RecyclePayAdapter adapter;
    private List<PayFees> list;
    private EditText editText;
    private boolean flag;
    private Handler handler;

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setTv_title("话费充值");
        viewDelegate.setOnClickListener(this, R.id.iv_back);
        editText = viewDelegate.getEt_phone();
        recyclerView = viewDelegate.getRecycle_hf();
        //设置gridLayout 样式
        recyclerView.setLayoutManager(new FullyGridLayoutManager(this, 3));
        adapter = new RecyclePayAdapter(this, list, flag);
        recyclerView.setAdapter(adapter);
        //设置recycle的间距一致
        recyclerView.addItemDecoration(new SpaceBaseItemDecoration(8));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        listener();
        editListener();
        handler();

    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                }
            }
        };
    }

    @Override
    protected void initData() {
        super.initData();
        flag = false;
        list = new ArrayList<PayFees>();
        String[] num = {"30", "50", "100", "200", "300", "500"};
        for (int i = 0; i < 6; i++) {
            PayFees payFees = new PayFees();
            payFees.setName(num[i]);
            payFees.setAliasName(num[i] + "元");
            list.add(payFees);
        }
    }

    /**
     * 电话号码输入监听
     */
    public void editListener() {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                //输入后
                String phoneStr = "";
                String c[] = s.toString().split(" ");
                for (int i = 0; i < c.length; i++) {
                    phoneStr = phoneStr + c[i];
                }
                if (phoneStr.length() > 7) {
                    String str1 = phoneStr.substring(0, 3);
                    String str2 = phoneStr.substring(3, 7);
                    String str3 = phoneStr.substring(7, phoneStr.length());
                    editText.removeTextChangedListener(this);
                    editText.setText(str1 + " " + str2 + " " + str3);
                } else if (phoneStr.length() > 3) {
                    String str1 = phoneStr.substring(0, 3);
                    String str2 = phoneStr.substring(3, phoneStr.length());
                    editText.removeTextChangedListener(this);
                    editText.setText(str1 + " " + str2);
                } else {
                    editText.removeTextChangedListener(this);
                    editText.setText(phoneStr);
                }
                editText.setSelection(editText.length());
                editText.addTextChangedListener(this);
                if (editText.getText().length() == 13) {
                    flag = true;
                    adapter.setData(flag);
                } else {
                    flag = false;
                    adapter.setData(flag);
                }
            }
        });

    }

    /**
     * recycleView监听
     */
    private void listener() {
        adapter.setOnItemClickLitener(new RecyclePayAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(PayFees payFees, int position) {
                if (flag) {
                    showMsg("充值" + payFees.getAliasName());
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;

        }

    }

    @Override
    protected Class<TeleChargeDelegate> getDelegateClass() {
        return TeleChargeDelegate.class;
    }
}
